package com.shard.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.shard.configentity.DataSource1Config;
import com.shard.configentity.DataSource2Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/4/1  8:49
 * 描述:
 *//*

public class ShardConfig {

     @Autowired
     private DataSource1Config dataSource1Config;

     @Autowired
     private DataSource2Config dataSource2Config;


    */
/**
     * 作者: LCG
     * 日期: 2021/4/1  8:50
     * 描述: 创建sharding数据源
     * @Param []
     * @Return javax.sql.DataSource
     *//*

    @Bean
    @Primary
    public DataSource getDataSource(){
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        //配置数据源1
        DruidDataSource druidDataSource1=new DruidDataSource();
        druidDataSource1.setUrl(dataSource1Config.getUrl());
        druidDataSource1.setUsername(dataSource1Config.getUsername());
        druidDataSource1.setPassword(dataSource1Config.getPassword());
        druidDataSource1.setDriverClassName(dataSource1Config.getDriverClassName());
        dataSourceMap.put("ds0",druidDataSource1);
        //配置数据源2
        DruidDataSource druidDataSource2=new DruidDataSource();
        druidDataSource2.setUrl(dataSource2Config.getUrl());
        druidDataSource2.setUsername(dataSource2Config.getUsername());
        druidDataSource2.setPassword(dataSource2Config.getPassword());
        druidDataSource2.setDriverClassName(dataSource2Config.getDriverClassName());
        dataSourceMap.put("ds1",druidDataSource2);

        //配置tb_order表规则
        ShardingTableRuleConfiguration shardingTableRuleConfiguration=
                new ShardingTableRuleConfiguration("tb_order","ds${0..1}.tb_order${0..1}");


        // 配置分库策略
        shardingTableRuleConfiguration.setDatabaseShardingStrategy(new
                StandardShardingStrategyConfiguration("userId", "dbShardingAlgorithm"));

        // 配置分表策略
        shardingTableRuleConfiguration.setTableShardingStrategy(new
                StandardShardingStrategyConfiguration("id", "tableShardingAlgorithm"));

        // 配置分⽚规则
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTables().add(shardingTableRuleConfiguration);

        // 配置分库算法
        Properties dbShardingAlgorithmrProps = new Properties();
        dbShardingAlgorithmrProps.setProperty("algorithm-expression", "ds${userId % 2}");
        shardingRuleConfig.getShardingAlgorithms().put("dbShardingAlgorithm", new
                ShardingSphereAlgorithmConfiguration("INLINE", dbShardingAlgorithmrProps));

        // 配置分表算法
        Properties tableShardingAlgorithmrProps = new Properties();
        tableShardingAlgorithmrProps.setProperty("algorithm-expression", "tb_order${id % 2}");
            shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", new
                                                           ShardingSphereAlgorithmConfiguration("INLINE", tableShardingAlgorithmrProps));

        // 创建 ShardingSphereDataSource



        return null;
    }

}
*/
